/**
 * 数字的十进制计算
 *
 * @param	{String}	type	计算类型，例如："round" | "floor" | "ceil"
 * @param	{Number}	value	数字.
 * @param	{Integer}	exp		基数，保留小数几位
 * @returns	{Number}			计算后的值.
 */
function decimalAdjust(type, value = 0, exp = -2) {
    // exp 为null或0
    exp = -Math.abs(exp);
    if (+exp === 0) {
        return Math[type](value);
    }
    //转换number
    value = +value;
    exp = +exp;
    // value不是数字类型，或exp不是整数 
    if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
        return NaN;
    }
    // Shift
    value = value.toString().split('e');
    value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp)));
    // Shift back
    value = value.toString().split('e');
    return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp));
}

const round = function(v, d) {
    return decimalAdjust('round', v, d);
}

var float = {
  round ,
  //向下取整计算
  floor: function(v, d) {
    return decimalAdjust('floor', v, d);
  },
  //向上取整计算
  ceil: function(v, d) {
    return decimalAdjust('ceil', v, d);
  },
  //相等
  eq: function(a, b, d) {
    return round(a, d) === round(b, d)
  },
  //小于
  lt: function(a, b, d) {
    return round(a, d) < round(b, d)
  },
  //大于
  gt: function(a, b, d) {
    return round(a, d) > round(b, d)
  },
  //小于等于
  lteq: function(a, b, d) {
    return round(a, d) <= round(b, d)
  },
  //大于等于
  gteq: function(a, b, d) {
    return round(a, d) >= round(b, d)
  },
}
export default float;